package iammry.com.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ViewInject;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

import iammry.com.R;
import iammry.com.base.BaseActivity;
import iammry.com.interfaceData.ITCloseCallBack;
import iammry.com.mode.LayerType;
import iammry.com.mode.SelectLayout;
import iammry.com.popuwindow.ActionSheetDialog;
import iammry.com.popuwindow.DialogWindows;
import iammry.com.utils.Bimp;
import iammry.com.utils.CircleImageView;
import iammry.com.utils.DataWidget;
import iammry.com.utils.FileUtils;

/**
 * @author kim
 * @data 2016-10-25
 * @todo TODO
 */
public class PersonInfoActivity extends BaseActivity implements OnClickListener {

    @ViewInject(R.id.tv_sex)
    TextView tv_sex;

    @ViewInject(R.id.tv_birthday)
    TextView tv_birthday;

    @ViewInject(R.id.bt_personinfo_commit)
    Button bt_personinfo_commit;

    @ViewInject(R.id.ll_sex)
    private LinearLayout ll_sex;

    @ViewInject(R.id.ll_birthday)
    private LinearLayout ll_birthday;

    @ViewInject(R.id.ll_toothcolor)
    private LinearLayout ll_toothcolor;

    @ViewInject(R.id.ll_back)
    private LinearLayout ll_back;

    @ViewInject(R.id.iv_head)
    private CircleImageView iv_head;

    @ViewInject(R.id.iv_sign)
    private ImageView iv_sign;

    @ViewInject(R.id.tv_toothcolor)
    private TextView tv_toothcolor;

    @ViewInject(R.id.ll_personinfo_root)
    private LinearLayout personinfo_root;

    private LinearLayout ll_boy;

    private LinearLayout ll_girl;

    private ImageView iv_sign_01;

    private ImageView iv_sign_02;

    int count = 0;

    private ImageView rb1;

    private ImageView rb2;

    private ImageView rb3;

    private ImageView rb4;

    private ImageView rb5;

    private ImageView rb6;

    private LinearLayout rg;

    private LinearLayout rb1_layout;

    private LinearLayout rb2_layout;

    private LinearLayout rb3_layout;

    private LinearLayout rb4_layout;

    private LinearLayout rb5_layout;

    private LinearLayout rb6_layout;

    private ImageView iv_image;

    DataWidget dataWidget;

    //上传照片params
    private static final int TAKE_PICTURE = 0;
    private static final int RESULT_LOAD_IMAGE = 1;
    private static final int CUT_PHOTO_REQUEST_CODE = 2;
    private static final int SELECTIMG_SEARCH = 3;
    private String path = "";
    String imagePath;
    private float dp;
    private Uri photoUri;

    @Override
    protected void initView() {

        setLayout(this, R.layout.personinfo_layout);
    }

    @Override
    protected void initData() {

        ll_sex.setOnClickListener(this);
        ll_birthday.setOnClickListener(this);
        bt_personinfo_commit.setOnClickListener(this);
        ll_toothcolor.setOnClickListener(this);
        iv_head.setOnClickListener(this);
        //ll_back.setVisibility(View.GONE);

    }

    @Override
    protected void initEvent() {

    }

    /**
     * 点击事件
     * @param view
     */
    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.iv_head:
                //头像的点击事件
                clickImageEvent();
                break;
            //性别选择
            case R.id.ll_sex:

                count++;

                View view1 = View.inflate(context, R.layout.sex_layout, null);

                initDialogLayerView(view1, LayerType.sex_layer);

                new SelectLayout(context, view1);

                initDialogLayerEvent(LayerType.sex_layer);

                break;
            //确定按钮
            case R.id.bt_personinfo_commit:

                startActivity(ScanBlueActivity.class);

                break;
            //生日选择
            case R.id.ll_birthday:

                dataWidget = new DataWidget();
                View dataView = dataWidget.getDataPick(getBaseContext());

                DialogWindows dialogWindows = new DialogWindows(context, new ITCloseCallBack() {
                    @Override
                    public void closeListening(boolean isClose) {

                        String data = dataWidget.getData();
                        tv_birthday.setText(data);
                    }
                });
                dialogWindows.builder();
                dialogWindows.setCancelable(true);
                dialogWindows.setCanceledOnTouchOutside(false);
                dialogWindows.setSheetItems(dataView);
                dialogWindows.show();

                break;
            //牙齿色泽选择
            case R.id.ll_toothcolor:

                // startActivity(ScanBlueActivity.class);
                View toothView = View.inflate(context, R.layout.toothcolor_layout,
                        null);

                initDialogLayerView(toothView, LayerType.toothcolor_layer);

                new SelectLayout(context, toothView);

                initDialogLayerEvent(LayerType.toothcolor_layer);

                break;

            default:
                break;
        }

    }

    /***
     * 初始化弹窗的布局
     * @param view
     * @param type
     */
    private void initDialogLayerView(View view, LayerType type) {

        if (type == LayerType.sex_layer) {

            ll_boy = (LinearLayout) view.findViewById(R.id.ll_boy);
            ll_girl = (LinearLayout) view.findViewById(R.id.ll_girl);
            iv_sign_01 = (ImageView) view.findViewById(R.id.iv_sign_01);
            iv_sign_02 = (ImageView) view.findViewById(R.id.iv_sign_02);

            if (tv_sex.getText() == "女") {

                iv_sign_01.setVisibility(View.GONE);
                iv_sign_02.setVisibility(View.VISIBLE);

            } else {

                iv_sign_01.setVisibility(View.VISIBLE);
                iv_sign_02.setVisibility(View.GONE);
            }

            if (count == 0) {

                tv_sex.setText("男");
            }

        } else if (type == LayerType.birthday_layer)

        {

        } else if (type == LayerType.toothcolor_layer) {

            rb1 = (ImageView) view.findViewById(R.id.rb1);
            rb2 = (ImageView) view.findViewById(R.id.rb2);
            rb3 = (ImageView) view.findViewById(R.id.rb3);
            rb4 = (ImageView) view.findViewById(R.id.rb4);
            rb5 = (ImageView) view.findViewById(R.id.rb5);
            rb6 = (ImageView) view.findViewById(R.id.rb6);
            rb1_layout = (LinearLayout) view
                    .findViewById(R.id.ll_rb1_layout);
            rb2_layout = (LinearLayout) view
                    .findViewById(R.id.ll_rb2_layout);
            rb3_layout = (LinearLayout) view
                    .findViewById(R.id.ll_rb3_layout);
            rb4_layout = (LinearLayout) view
                    .findViewById(R.id.ll_rb4_layout);
            rb5_layout = (LinearLayout) view
                    .findViewById(R.id.ll_rb5_layout);
            rb6_layout = (LinearLayout) view
                    .findViewById(R.id.ll_rb6_layout);
            rg = (LinearLayout) view.findViewById(R.id.rg);
            iv_image = (ImageView) view.findViewById(R.id.iv_image);

            //RotationImage();

            clickEvent(1);

        }

    }

    /**
     * 初始化弹窗事件
     * @param type
     */
    private void initDialogLayerEvent(LayerType type) {

        if (type == LayerType.sex_layer) {
            ll_boy.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    iv_sign_01.setVisibility(View.VISIBLE);
                    iv_sign_02.setVisibility(View.GONE);
                    tv_sex.setText("男");
                    iv_sign.setBackgroundResource(R.mipmap.sign_tie_boy);
                }
            });
            ll_girl.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    iv_sign_02.setVisibility(View.VISIBLE);
                    iv_sign_01.setVisibility(View.GONE);
                    tv_sex.setText("女");
                    iv_sign.setBackgroundResource(R.mipmap.sign_tie_girl);
                }
            });
        }
        // 布局
        else if (type == LayerType.toothcolor_layer) {

            rb1_layout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    clickEvent(1);
                }
            });
            rb2_layout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    clickEvent(2);

                }
            });
            rb3_layout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    clickEvent(3);

                }
            });
            rb4_layout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    clickEvent(4);

                }
            });
            rb5_layout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    clickEvent(5);

                }
            });
            rb6_layout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    clickEvent(6);

                }
            });


        }

    }

    //点击图片事件
    public void clickImageEvent() {

        //获取选项的名字
        String[] take_photos = getResources().getStringArray(
                R.array.take_photo);

        new ActionSheetDialog(this)
                .builder()
                .setTitle(take_photos[0])
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem(take_photos[1], ActionSheetDialog.SheetItemColor.Black,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                //拍照
                                photo();
                            }
                        })
                .addSheetItem(take_photos[2], ActionSheetDialog.SheetItemColor.Black,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                //获取本地相册

                                Intent i = new Intent(
                                        Intent.ACTION_PICK,
                                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                                startActivityForResult(i, RESULT_LOAD_IMAGE);

                            }
                        }).show();

    }


    /**
     * 开启图库的裁剪
     * @param uri 照片的uri
     */
    private void startPhotoZoom(Uri uri) {
        try {
            //格式化日期
            SimpleDateFormat sDateFormat = new SimpleDateFormat(
                    "yyyyMMddhhmmss");

            String address = sDateFormat.format(new java.util.Date());
            if (!FileUtils.isFileExist("")) {
                FileUtils.createSDDir("");

            }

            imagePath = FileUtils.SDPATH + address + ".JPEG";

            Uri imageUri = Uri.parse("file:///sdcard/formats/" + address
                    + ".JPEG");

            final Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(uri, "image/*");
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("outputX", 280);
            intent.putExtra("outputY", 280);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            intent.putExtra("outputFormat",
                    Bitmap.CompressFormat.JPEG.toString());
            intent.putExtra("noFaceDetection", false);
            intent.putExtra("return-data", false);
            startActivityForResult(intent, CUT_PHOTO_REQUEST_CODE);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 调起本地的相册
     */
    public void photo() {
        try {
            Intent openCameraIntent = new Intent(
                    MediaStore.ACTION_IMAGE_CAPTURE);
            String sdcardState = Environment.getExternalStorageState();
            String sdcardPathDir = android.os.Environment
                    .getExternalStorageDirectory().getPath() + "/tempImage/";
            File file = null;
            if (Environment.MEDIA_MOUNTED.equals(sdcardState)) {
                File fileDir = new File(sdcardPathDir);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }

                file = new File(sdcardPathDir + System.currentTimeMillis()
                        + ".JPEG");
            }
            if (file != null) {
                path = file.getPath();
                photoUri = Uri.fromFile(file);
                openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                startActivityForResult(openCameraIntent, TAKE_PICTURE);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 接收相册选取图片返回值
     * @param requestCode
     * @param resultCode
     * @param data
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case TAKE_PICTURE:
                if (resultCode == -1) {
                    startPhotoZoom(photoUri);
                }
                break;
            case RESULT_LOAD_IMAGE:

                if (resultCode == RESULT_OK && null != data) {//
                    Uri uri = data.getData();
                    if (uri != null) {
                        startPhotoZoom(uri);
                    }
                }
                break;
            case CUT_PHOTO_REQUEST_CODE:

                if (resultCode == RESULT_OK && null != data) {//

                    System.out.println("imagePath=" + imagePath);

                    Bitmap bitmap = Bimp.getLoacalBitmap(imagePath);

                    System.out.println("bitmap=" + bitmap);
                    //PhotoActivity.bitmap.add(bitmap);
                    bitmap = Bimp.createFramedPhoto(240, 240, bitmap,
                            (int) (dp * 1.6f));

                    System.out.println("bitmap=" + bitmap);

                    iv_head.setImageBitmap(bitmap);

                }
                break;
            case SELECTIMG_SEARCH:


                break;
        }
    }
//日期回调函数


    //点击事件
    public void clickEvent(int type) {

        switch (type) {

            case 1:
                rb1.setBackgroundResource(R.mipmap.bat_btn_choose_h1);
                rb2.setBackgroundResource(R.mipmap.bat_btn_choose_n2);
                rb3.setBackgroundResource(R.mipmap.bat_btn_choose_n3);
                rb4.setBackgroundResource(R.mipmap.bat_btn_choose_n4);
                rb5.setBackgroundResource(R.mipmap.bat_btn_choose_n5);
                rb6.setBackgroundResource(R.mipmap.bat_btn_choose_n6);
                tv_toothcolor.setText("抽烟人群");
                break;
            case 2:
                rb2.setBackgroundResource(R.mipmap.bat_btn_choose_h2);
                rb1.setBackgroundResource(R.mipmap.bat_btn_choose_n1);
                rb3.setBackgroundResource(R.mipmap.bat_btn_choose_n3);
                rb4.setBackgroundResource(R.mipmap.bat_btn_choose_n4);
                rb5.setBackgroundResource(R.mipmap.bat_btn_choose_n5);
                rb6.setBackgroundResource(R.mipmap.bat_btn_choose_n6);
                tv_toothcolor.setText("咖啡人群");
                break;
            case 3:

                rb3.setBackgroundResource(R.mipmap.bat_btn_choose_h3);
                rb1.setBackgroundResource(R.mipmap.bat_btn_choose_n1);
                rb2.setBackgroundResource(R.mipmap.bat_btn_choose_n2);
                rb4.setBackgroundResource(R.mipmap.bat_btn_choose_n4);
                rb5.setBackgroundResource(R.mipmap.bat_btn_choose_n5);
                rb6.setBackgroundResource(R.mipmap.bat_btn_choose_n6);
                tv_toothcolor.setText("红酒人群");
                break;
            case 4:
                rb4.setBackgroundResource(R.mipmap.bat_btn_choose_h4);
                rb1.setBackgroundResource(R.mipmap.bat_btn_choose_n1);
                rb2.setBackgroundResource(R.mipmap.bat_btn_choose_n2);
                rb3.setBackgroundResource(R.mipmap.bat_btn_choose_n3);
                rb5.setBackgroundResource(R.mipmap.bat_btn_choose_n5);
                rb6.setBackgroundResource(R.mipmap.bat_btn_choose_n6);
                tv_toothcolor.setText("可乐人群");
                break;
            case 5:
                rb5.setBackgroundResource(R.mipmap.bat_btn_choose_h5);
                rb1.setBackgroundResource(R.mipmap.bat_btn_choose_n1);
                rb2.setBackgroundResource(R.mipmap.bat_btn_choose_n2);
                rb3.setBackgroundResource(R.mipmap.bat_btn_choose_n3);
                rb4.setBackgroundResource(R.mipmap.bat_btn_choose_n4);
                rb6.setBackgroundResource(R.mipmap.bat_btn_choose_n6);
                tv_toothcolor.setText("饮茶人群");
                break;
            case 6:
                rb6.setBackgroundResource(R.mipmap.bat_btn_choose_h6);
                rb1.setBackgroundResource(R.mipmap.bat_btn_choose_n1);
                rb2.setBackgroundResource(R.mipmap.bat_btn_choose_n2);
                rb3.setBackgroundResource(R.mipmap.bat_btn_choose_n3);
                rb4.setBackgroundResource(R.mipmap.bat_btn_choose_n4);
                rb5.setBackgroundResource(R.mipmap.bat_btn_choose_n5);
                tv_toothcolor.setText("吃饭人群");
                break;
            default:
                break;
        }
    }
}
